0

Implementation of Chatbot using NLP.ipynb

No Headings

The table of contents shows headings in notebooks and supported files.

Skip to Main
Jupyter

Implementation of Chatbot using NLP

Last Checkpoint: 23 minutes ago
  • File
  • Edit
  • View
  • Run
  • Kernel
  • Settings
  • Help
JupyterLab
Python [conda env:base] *
Kernel status: Idle Executed 2 cellsElapsed time: 194 seconds
image/svg+xml
  1. 1
  2. 2
  3. 3
  4. 4
  5. 5
  6. 6
[1]:
pip install nltk scikit-learn streamlit
Requirement already satisfied: nltk in c:\users\sweety\anaconda3\lib\site-packages (3.9.1)
Requirement already satisfied: scikit-learn in c:\users\sweety\anaconda3\lib\site-packages (1.5.1)
Requirement already satisfied: streamlit in c:\users\sweety\anaconda3\lib\site-packages (1.37.1)
Requirement already satisfied: click in c:\users\sweety\anaconda3\lib\site-packages (from nltk) (8.1.7)
Requirement already satisfied: joblib in c:\users\sweety\anaconda3\lib\site-packages (from nltk) (1.4.2)
Requirement already satisfied: regex>=2021.8.3 in c:\users\sweety\anaconda3\lib\site-packages (from nltk) (2024.9.11)
Requirement already satisfied: tqdm in c:\users\sweety\anaconda3\lib\site-packages (from nltk) (4.66.5)
Requirement already satisfied: numpy>=1.19.5 in c:\users\sweety\anaconda3\lib\site-packages (from scikit-learn) (1.26.4)
Requirement already satisfied: scipy>=1.6.0 in c:\users\sweety\anaconda3\lib\site-packages (from scikit-learn) (1.13.1)
Requirement already satisfied: threadpoolctl>=3.1.0 in c:\users\sweety\anaconda3\lib\site-packages (from scikit-learn) (3.5.0)
Requirement already satisfied: altair<6,>=4.0 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (5.0.1)
Requirement already satisfied: blinker<2,>=1.0.0 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (1.6.2)
Requirement already satisfied: cachetools<6,>=4.0 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (5.3.3)
Requirement already satisfied: packaging<25,>=20 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (24.1)
Requirement already satisfied: pandas<3,>=1.3.0 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (2.2.2)
Requirement already satisfied: pillow<11,>=7.1.0 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (10.4.0)
Requirement already satisfied: protobuf<6,>=3.20 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (4.25.3)
Requirement already satisfied: pyarrow>=7.0 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (16.1.0)
Requirement already satisfied: requests<3,>=2.27 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (2.32.3)
Requirement already satisfied: rich<14,>=10.14.0 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (13.7.1)
Requirement already satisfied: tenacity<9,>=8.1.0 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (8.2.3)
Requirement already satisfied: toml<2,>=0.10.1 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (0.10.2)
Requirement already satisfied: typing-extensions<5,>=4.3.0 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (4.11.0)
Requirement already satisfied: gitpython!=3.1.19,<4,>=3.0.7 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (3.1.43)
Requirement already satisfied: pydeck<1,>=0.8.0b4 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (0.8.0)
Requirement already satisfied: tornado<7,>=6.0.3 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (6.4.1)
Requirement already satisfied: watchdog<5,>=2.1.5 in c:\users\sweety\anaconda3\lib\site-packages (from streamlit) (4.0.1)
Requirement already satisfied: jinja2 in c:\users\sweety\anaconda3\lib\site-packages (from altair<6,>=4.0->streamlit) (3.1.4)
Requirement already satisfied: jsonschema>=3.0 in c:\users\sweety\anaconda3\lib\site-packages (from altair<6,>=4.0->streamlit) (4.23.0)
Requirement already satisfied: toolz in c:\users\sweety\anaconda3\lib\site-packages (from altair<6,>=4.0->streamlit) (0.12.0)
Requirement already satisfied: colorama in c:\users\sweety\anaconda3\lib\site-packages (from click->nltk) (0.4.6)
Requirement already satisfied: gitdb<5,>=4.0.1 in c:\users\sweety\anaconda3\lib\site-packages (from gitpython!=3.1.19,<4,>=3.0.7->streamlit) (4.0.7)
Requirement already satisfied: python-dateutil>=2.8.2 in c:\users\sweety\anaconda3\lib\site-packages (from pandas<3,>=1.3.0->streamlit) (2.9.0.post0)
Requirement already satisfied: pytz>=2020.1 in c:\users\sweety\anaconda3\lib\site-packages (from pandas<3,>=1.3.0->streamlit) (2024.1)
Requirement already satisfied: tzdata>=2022.7 in c:\users\sweety\anaconda3\lib\site-packages (from pandas<3,>=1.3.0->streamlit) (2023.3)
Requirement already satisfied: charset-normalizer<4,>=2 in c:\users\sweety\anaconda3\lib\site-packages (from requests<3,>=2.27->streamlit) (3.3.2)
Requirement already satisfied: idna<4,>=2.5 in c:\users\sweety\anaconda3\lib\site-packages (from requests<3,>=2.27->streamlit) (3.7)
Requirement already satisfied: urllib3<3,>=1.21.1 in c:\users\sweety\anaconda3\lib\site-packages (from requests<3,>=2.27->streamlit) (2.2.3)
Requirement already satisfied: certifi>=2017.4.17 in c:\users\sweety\anaconda3\lib\site-packages (from requests<3,>=2.27->streamlit) (2024.12.14)
Requirement already satisfied: markdown-it-py>=2.2.0 in c:\users\sweety\anaconda3\lib\site-packages (from rich<14,>=10.14.0->streamlit) (2.2.0)
Requirement already satisfied: pygments<3.0.0,>=2.13.0 in c:\users\sweety\anaconda3\lib\site-packages (from rich<14,>=10.14.0->streamlit) (2.15.1)
Requirement already satisfied: smmap<5,>=3.0.1 in c:\users\sweety\anaconda3\lib\site-packages (from gitdb<5,>=4.0.1->gitpython!=3.1.19,<4,>=3.0.7->streamlit) (4.0.0)
Requirement already satisfied: MarkupSafe>=2.0 in c:\users\sweety\anaconda3\lib\site-packages (from jinja2->altair<6,>=4.0->streamlit) (2.1.3)
Requirement already satisfied: attrs>=22.2.0 in c:\users\sweety\anaconda3\lib\site-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (23.1.0)
Requirement already satisfied: jsonschema-specifications>=2023.03.6 in c:\users\sweety\anaconda3\lib\site-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (2023.7.1)
Requirement already satisfied: referencing>=0.28.4 in c:\users\sweety\anaconda3\lib\site-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (0.30.2)
Requirement already satisfied: rpds-py>=0.7.1 in c:\users\sweety\anaconda3\lib\site-packages (from jsonschema>=3.0->altair<6,>=4.0->streamlit) (0.10.6)
Requirement already satisfied: mdurl~=0.1 in c:\users\sweety\anaconda3\lib\site-packages (from markdown-it-py>=2.2.0->rich<14,>=10.14.0->streamlit) (0.1.0)
Requirement already satisfied: six>=1.5 in c:\users\sweety\anaconda3\lib\site-packages (from python-dateutil>=2.8.2->pandas<3,>=1.3.0->streamlit) (1.16.0)
Note: you may need to restart the kernel to use updated packages.
[3]:
import nltk
import random
import numpy as np
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.linear_model import LogisticRegression
from nltk.stem import WordNetLemmatizer
[nltk_data] Downloading package punkt_tab to
[nltk_data]     C:\Users\Sweety\AppData\Roaming\nltk_data...
[nltk_data]   Package punkt_tab is already up-to-date!
[nltk_data] Downloading package wordnet to
[nltk_data]     C:\Users\Sweety\AppData\Roaming\nltk_data...
[nltk_data]   Package wordnet is already up-to-date!
[22]:
all_patterns = []
all_labels = []
responses = {}

for intent in intents:
for pattern in intent['patterns']:
all_patterns.append(pattern)
all_labels.append(intent['tag'])
responses[intent['tag']] = intent['responses']

def clean_up_sentence(sentence):
sentence_words = nltk.word_tokenize(sentence)
sentence_words = [lemmatizer.lemmatize(w.lower()) for w in sentence_words]
return sentence_words

vectorizer = TfidfVectorizer(tokenizer=clean_up_sentence)
X = vectorizer.fit_transform(all_patterns)

classifier = LogisticRegression()
classifier.fit(X, all_labels)

def get_response(user_input):
input_vector = vectorizer.transform([user_input])
predicted_tag = classifier.predict(input_vector)[0]
response = random.choice(responses[predicted_tag])
return response

while True:
user_input = input("You: ")
if user_input.lower() == "quit":
break
response = get_response(user_input)
print(f"Bot: {response}")
C:\Users\Sweety\anaconda3\Lib\site-packages\sklearn\feature_extraction\text.py:521: UserWarning: The parameter 'token_pattern' will not be used since 'tokenizer' is not None'
  warnings.warn(
You:  Hi
Bot: Hello
You:  whats up?
Bot: Hi there
You:  who are you
Bot: I can answer questions and provide assistance
You:  what is your purpose
Bot: I am a chatbot
You:  Can you help me
Bot: Sure, what do you need help with?
You:  How's the weather today
Bot: You can check the weather on a weather app or website.
You:  How can I make a budget
Bot: To create a budget, start by setting financial goals for yourself. Then, track your income and expenses for a few months to get a sense of where your money is going. Next, create a budget by allocating your income towards essential expenses, savings and debt repayment, and discretionary expenses.
You:  what is credit score
Bot: You can check your credit score for free on several websites such as Credit Karma and Credit Sesame.
You:  who is your developer
Bot: suraj sharma created me
You:  please play a song for me
Bot: Yes sure! which song do you loke to play
You:  bye
Bot: Goodbye
You:  quit
Common Tools
No metadata.
Advanced Tools
No metadata.
Anaconda Assistant
AI-powered coding, insights and debugging in your notebooks.
To enable the following extensions, create an account or sign in.
  • Anaconda Assistant
    4.1.0
  • Coming soon!
  • Data Catalogs
  • Panel Deployments
  • Sharing
Already have an account? Sign In
For more information, read our Anaconda Assistant documentation.
Alt+[
Alt+]
Alt+End
  • Assistant
  • Open Anaconda Assistant
    Ctrl+Shift+A
  • Console
  • Change Kernel…
  • Clear Console Cells
  • Close and Shut Down…
  • Insert Line Break
  • Interrupt Kernel
  • New Console
  • Restart Kernel…
  • Run Cell (forced)
  • Run Cell (unforced)
  • Show All Kernel Activity
  • Display Languages
  • English
    English
  • File Operations
  • Autosave Documents
  • Download
    Download the file to your computer
  • Reload Notebook from Disk
    Reload contents from disk
  • Revert Notebook to Checkpoint…
    Revert contents to previous checkpoint
  • Save Notebook
    Save and create checkpoint
    Ctrl+S
  • Save Notebook As…
    Save with new path
    Ctrl+Shift+S
  • Trust HTML File
    Whether the HTML file is trusted. Trusting the file allows scripts to run in it, which may result in security risks. Only enable for files you trust.
  • Help
  • About Jupyter Notebook
  • Jupyter Reference
  • JupyterLab FAQ
  • JupyterLab Reference
  • Launch Jupyter Notebook File Browser
  • Markdown Reference
  • Show Keyboard Shortcuts…
    Show relevant keyboard shortcuts for the current active widget
    Ctrl+Shift+H
  • Image Viewer
  • Flip image horizontally
    H
  • Flip image vertically
    V
  • Invert Colors
    I
  • Reset Image
    0
  • Rotate Clockwise
    ]
  • Rotate Counterclockwise
    [
  • Zoom In
    =
  • Zoom Out
    -
  • Kernel Operations
  • Shut Down All Kernels…
  • Main Area
  • Close All Other Tabs
  • Close Tab
    Alt+W
  • Close Tabs to Right
  • End Search
    Esc
  • Find Next
    Ctrl+G
  • Find Previous
    Ctrl+Shift+G
  • Find…
    Ctrl+F
  • Log Out
    Log out of Jupyter Notebook
  • Search in Selection
    Alt+L
  • Shut Down
    Shut down Jupyter Notebook
  • Mode
  • Toggle Zen Mode
  • Notebook Cell Operations
  • Change to Code Cell Type
    Y
  • Change to Heading 1
    1
  • Change to Heading 2
    2
  • Change to Heading 3
    3
  • Change to Heading 4
    4
  • Change to Heading 5
    5
  • Change to Heading 6
    6
  • Change to Markdown Cell Type
    M
  • Change to Raw Cell Type
    R
  • Clear Cell Output
    Clear outputs for the selected cells
  • Collapse All Code
  • Collapse All Outputs
  • Collapse Selected Code
  • Collapse Selected Outputs
  • Copy Cell
    Copy this cell
    C
  • Cut Cell
    Cut this cell
    X
  • Delete Cell
    Delete this cell
    D, D
  • Disable Scrolling for Outputs
  • Enable Scrolling for Outputs
  • Expand All Code
  • Expand All Outputs
  • Expand Selected Code
  • Expand Selected Outputs
  • Extend Selection Above
    Shift+K
  • Extend Selection Below
    Shift+J
  • Extend Selection to Bottom
    Shift+End
  • Extend Selection to Top
    Shift+Home
  • Insert Cell Above
    Insert a cell above
    A
  • Insert Cell Below
    Insert a cell below
    B
  • Insert Heading Above Current Heading
    Shift+A
  • Insert Heading Below Current Heading
    Shift+B
  • Merge Cell Above
    Ctrl+Backspace
  • Merge Cell Below
    Ctrl+Shift+M
  • Merge Selected Cells
    Shift+M
  • Move Cell Down
    Move this cell down
    Ctrl+Shift+Down
  • Move Cell Up
    Move this cell up
    Ctrl+Shift+Up
  • Paste Cell Above
    Paste this cell from the clipboard
  • Paste Cell and Replace
  • Paste Cell Below
    Paste this cell from the clipboard
    V
  • Redo Cell Operation
    Shift+Z
  • Render Side-by-Side
    Shift+R
  • Run Selected Cell
    Run this cell and advance
    Shift+Enter
  • Run Selected Cell and Do not Advance
    Ctrl+Enter
  • Run Selected Cell and Insert Below
    Alt+Enter
  • Run Selected Text or Current Line in Console
  • Select Cell Above
    K
  • Select Cell Below
    J
  • Select Heading Above or Collapse Heading
    Left
  • Select Heading Below or Expand Heading
    Right
  • Set side-by-side ratio
  • Split Cell
    Ctrl+Shift+-
  • Undo Cell Operation
    Z
  • Notebook Operations
  • Access Next Kernel History Entry
    Alt+Down
  • Access Previous Kernel History Entry
    Alt+Up
  • Change Kernel…
  • Clear Outputs of All Cells
    Clear all outputs of all cells
  • Close and Shut Down Notebook…
  • Collapse All Headings
    Ctrl+Shift+Left
  • Deselect All Cells
  • Edit Notebook Metadata
  • Enter Command Mode
    Ctrl+M
  • Enter Edit Mode
    Enter
  • Expand All Headings
    Ctrl+Shift+Right
  • Interrupt Kernel
    Interrupt the kernel
  • New Console for Notebook
  • New Notebook
    Create a new notebook
  • Open with Panel in New Browser Tab
  • Preview Notebook with Panel
  • Reconnect to Kernel
  • Render All Markdown Cells
  • Restart Kernel and Clear Outputs of All Cells…
    Restart the kernel and clear all outputs of all cells
  • Restart Kernel and Debug…
    Restart Kernel and Debug…
  • Restart Kernel and Run All Cells…
    Restart the kernel and run all cells
  • Restart Kernel and Run up to Selected Cell…
  • Restart Kernel…
    Restart the kernel
  • Run All Above Selected Cell
  • Run All Cells
    Run all cells
  • Run Selected Cell and All Below
  • Save and Export Notebook: Asciidoc
  • Save and Export Notebook: Executable Script
  • Save and Export Notebook: HTML
  • Save and Export Notebook: LaTeX
  • Save and Export Notebook: Markdown
  • Save and Export Notebook: PDF
  • Save and Export Notebook: Qtpdf
  • Save and Export Notebook: Qtpng
  • Save and Export Notebook: ReStructured Text
  • Save and Export Notebook: Reveal.js Slides
  • Save and Export Notebook: Webpdf
  • Select All Cells
    Ctrl+A
  • Show Line Numbers
  • Toggle Collapse Notebook Heading
  • Trust Notebook
  • Other
  • Open in JupyterLab
    JupyterLab
  • Plugin Manager
  • Advanced Plugin Manager
  • Terminal
  • Decrease Terminal Font Size
  • Increase Terminal Font Size
  • New Terminal
    Start a new terminal session
  • Refresh Terminal
    Refresh the current terminal session
  • Use Terminal Theme: Dark
    Set the terminal theme
  • Use Terminal Theme: Inherit
    Set the terminal theme
  • Use Terminal Theme: Light
    Set the terminal theme
  • Text Editor
  • Decrease Font Size
  • Increase Font Size
  • New Markdown File
    Create a new markdown file
  • New Python File
    Create a new Python file
  • New Text File
    Create a new text file
  • Spaces: 1
  • Spaces: 2
  • Spaces: 4
  • Spaces: 4
  • Spaces: 8
  • Theme
  • Decrease Code Font Size
  • Decrease Content Font Size
  • Decrease UI Font Size
  • Increase Code Font Size
  • Increase Content Font Size
  • Increase UI Font Size
  • Set Preferred Dark Theme: JupyterLab Dark
  • Set Preferred Dark Theme: JupyterLab Dark High Contrast
  • Set Preferred Dark Theme: JupyterLab Light
  • Set Preferred Light Theme: JupyterLab Dark
  • Set Preferred Light Theme: JupyterLab Dark High Contrast
  • Set Preferred Light Theme: JupyterLab Light
  • Synchronize Styling Theme with System Settings
  • Theme Scrollbars
  • Use Theme: JupyterLab Dark
  • Use Theme: JupyterLab Dark High Contrast
  • Use Theme: JupyterLab Light
  • View
  • File Browser
  • Open JupyterLab
  • Show Anaconda Assistant
    Show Show Anaconda Assistant in the right sidebar
  • Show Header
  • Show Notebook Tools
    Show Show Notebook Tools in the right sidebar
  • Show Table of Contents
    Show Show Table of Contents in the left sidebar